package com.atguigu.wordcount;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;

import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;


/**
 * @author wky
 * @create 2021-07-12-11:42
 */
public class Count2 {
    public static void main(String[] args) throws Exception {
        //创建执行环境
        StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
        //将线程数设置为1
        //senv.setParallelism(1); //学习时好看一点 实际需要按照需求
        //批处理 在有界流式提高效率 将结果算出后提交
        senv.setRuntimeMode(RuntimeExecutionMode.BATCH);
        //读取数据
        DataStreamSource<String> stringDataStreamSource = senv.readTextFile("src/input/word.txt");
//        FlatMapOperator<String, Tuple2<String, Long>> wordsToOne = stringDataStreamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
//            @Override
//            public void flatMap(String s, Collector<Tuple2<String, Long>> collector) throws Exception {
//                String[] words = s.split(" ");
//                for (String word : words) {
//                    collector.collect(Tuple2.of(word, 1L));
//                }
//            }
//        });
        //解析数据
        SingleOutputStreamOperator<Tuple2<String, Long>> wordsToOne = stringDataStreamSource.
            flatMap((FlatMapFunction<String, Tuple2<String, Long>>) (s, collector) -> {
            String[] words = s.split(" ");
            for (String word : words) {
                collector.collect(Tuple2.of(word, 1L));
            }
        }).returns(Types.TUPLE(Types.STRING, Types.LONG));
        KeyedStream<Tuple2<String, Long>, Tuple> tuple2TupleKeyedStream = wordsToOne.keyBy(0);
        SingleOutputStreamOperator<Tuple2<String, Long>> sum = tuple2TupleKeyedStream.sum(1);
        //打印数据
        sum.print();
        //执行程序
        senv.execute();


    }
}
